﻿using AutojsController.Config;
using AutojsController.Helpers;
using AutojsController.Services;
using Avalonia;
using Avalonia.Controls.Platform;
using ReactiveUI;
using Serilog;
using SukiUI.Dialogs;
using SukiUI.Toasts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace AutojsController.ViewModels
{
    public class AddOrRenameGroupDialogViewModel:ReactiveObject
    {
        private ISukiDialog dialog;
        private ISukiToastManager toastManager;
        private Group? group;
        private GroupsService grpService;
        private readonly ILogger logger = LoggerFactory.GetLogger();

        public int MaxGroupNameLength {
            get => 10;
        }
        public string TextboxWatermark
        {
            get => group == null ? $"最长{MaxGroupNameLength}个字符" : $"旧分组名: {group.Name}";
        }

        public string Title
        {
            get => group == null ? "新建分组名" : "重命名分组";
        }

        private string groupName = string.Empty;
        public string GroupName
        {
            get => group == null ? "" : group.Name;
            set => this.RaiseAndSetIfChanged(ref groupName, value.Trim());
        }

        public ICommand CloseCommand => ReactiveCommand.Create(close);
        public ICommand SaveCommand => ReactiveCommand.Create(save);

        public event Action<bool>? Closed;

        /// <summary>
        /// 如果没给group就是新建，如果指定group就是重命名
        /// </summary>
        public AddOrRenameGroupDialogViewModel(ISukiDialog dlg, ISukiToastManager toast, Group? group, GroupsService grpService)
        {
            this.dialog = dlg;
            this.toastManager = toast;
            this.group = group;
            this.grpService = grpService;
        }

        private void close()
        {
            Closed?.Invoke(false);
            dialog.Dismiss();
        }

        private async Task save()
        {
            if (groupName.Length == 0)
            {
                SukiToast.Error(toastManager, "请输入分组名", 3);
            }
            else
            {
                if (group == null)
                {
                    try
                    {
                        var mobile = await grpService.AddGroupAsync(groupName, null);
                        logger.Information($"添加分组{groupName}");
                        Closed?.Invoke(true);
                    }
                    catch (Exception ex) when (ex is DuplicateGroupException || ex is GroupCountLimitExceededException)
                    {
                        SukiToast.Error(toastManager, ex.Message, 3);
                    }
                    catch
                    {
                        SukiToast.Error(toastManager, "未知错误", 3);
                        dialog.Dismiss();
                        return;
                    }
                }
                else if (group.Name != groupName)
                {
                    await grpService.RenameAsync(group.Id, groupName);
                    logger.Information($"将分组{group.Name}重命名为{GroupName}");
                    Closed?.Invoke(true);
                }
                else
                    Closed?.Invoke(false);
                dialog.Dismiss();
            }
        }
    }
}
